import java.awt.Graphics;
import java.util.Map;

import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSObject;

public class AutoSplitter extends Script implements PaintListener,
		ServerMessageListener {
	static int chisel = 1755;
	static int[] bankbooth = { 11402, 35647, 36786, 25808, 34752, 11758, 14367,
			26972, 11758, 2213 };
	int state = 0;
	int logid = 6981;
	int bowid = 6979;
	int inter = 305;
	int x = 275, y = 405;
	int i = 0;
	int a = 0;
	ServerMessageEvent e;
	String msg = "";
	int none = 0;
	int force = 0;
	public int twokg;
	int granite;
	public int cut = 0;
	public long startTime = System.currentTimeMillis();
	public int startLevel = skills.getCurrentSkillLevel(9);
	public int startXP = skills.getCurrentSkillExp(9);
	public String block = "";

	@Override
	public String getAuthor() {
		return "thesvcoolman";
	}

	@Override
	public String getName() {
		return "Auto Granite Splitter";
	}

	@Override
	public String getScriptCategory() {
		return "Money";
	}

	@Override
	public String getScriptDescription() {
		String html = "";
		html += "<html>";
		html += "<center>";
		html += "<h2>" + getName() + "</h2>";
		html += "<br />";
		html += "<b>Author:</b> " + getAuthor();
		html += "<br />";
		html += "<b>Version:</b> " + getVersion();
		html += "<br /><br />";
		html += "<b>Instructions:</b>";
		html += "<br />";
		html += "Start point: any bank (I hope)";
		html += "<br />";
		html += "Inventory: ONLY a chisel";
		html += "<br />";
		html += "<br />";
		html += "<b>Arguments:</b>";
		html += "<select name=\"blocks\">";
		html += "<option value=\"5Kg\">5kg</option>";
		html += "<option value=\"2Kg\">2kg</option>";
		html += "</select>";
		html += "<br />";
		html += "Report possible bugs at thesvcoolman@yahoo.com.";
		html += "<br />";
		html += "<br />";
		html += "<br />";
		html += "I used AutoFletching v0.1 as source for this server, so it isn't mine 100%.";
		html += "</center>";
		html += "</html>";
		return html;
	}

	@Override
	public double getVersion() {
		return 1.0;
	}

	@Override
	public int loop() {
		if (RSInterface.getInterface(378).isValid()) {
			wait(100);
		}
		switch (state) {
		case 0: // banking
		{
			final RSObject Bank = findObject(AutoSplitter.bankbooth);
			if (distanceTo(Bank.getLocation()) > 3) {
				walkTileMM(Bank.getLocation());
				return random(250, 500);
			}
			if (!RSInterface.getInterface(762).isValid() && Bank != null) {
				while (!RSInterface.getInterface(762).isValid()) {
					atObject(Bank, "Use-quickly");
					wait(random(250, 500));
				}
				bank.depositAllExcept(AutoSplitter.chisel);
				if (twokg == 1) {
					clickMouse(random(42, 62), random(100, 115), false);
					wait(random(300, 600));
					atMenu("Withdraw-5");
					wait(random(50, 55));
					clickMouse(random(42, 62), random(100, 115), true);
				} else // twokg=0
				{

					clickMouse(random(42, 62), random(100, 115), true);
					wait(random(50, 55));
					clickMouse(random(42, 62), random(100, 115), true);
				}
				wait(random(100, 150));
				bank.close();
				state = 1;
				return random(50, 100);
			}
		}
		case 1: // splitting
		{
			if (isLoggedIn() && getCurrentTab() != Constants.TAB_INVENTORY) {
				openTab(Constants.TAB_INVENTORY);
			}
			if (!inventoryContains(granite) && !inventoryContains(6981)) {
				state = 0;
				return random(100, 200);
			}
			while (inventoryContains(granite)) {
				atInventoryItem(granite, "Use");
				wait(random(5, 20));
				atInventoryItem(AutoSplitter.chisel, "Use");
				wait(random(75, 100));
				while (!RSInterface.getInterface(228).isValid() && force == 0) {

					wait(50);
					a++;
					if (a > 50) {
						force++;
					}
				}
				force = 0;
				a = 0;
				clickMouse(x + random(120, 135), y, true);
				wait(random(10, 10));
				wait(random(10, 20));
				i++;
			}
			while (inventoryContains(6981)) {
				atInventoryItem(6981, "Use");
				wait(random(5, 20));
				atInventoryItem(AutoSplitter.chisel, "Use");
				wait(random(75, 100));
				i++;
				while (!RSInterface.getInterface(228).isValid() && force == 0) {
					wait(50);
					a++;
					if (a > 50) {
						force++;
					}
				}
				force = 0;
				a = 0;
				clickMouse(x + random(120, 135), y, true);
				wait(random(10, 10));
				wait(random(10, 20));
			}
			i = 0;
			state = 0;
			return random(100, 150);
		}
		}
		log("Error!");
		return -1;
	}

	public void onRepaint(final Graphics g) {
		if (isLoggedIn()) {
			long millis = System.currentTimeMillis() - startTime;
			final long hours = millis / (1000 * 60 * 60);
			millis -= hours * 1000 * 60 * 60;
			final long minutes = millis / (1000 * 60);
			millis -= minutes * 1000 * 60;
		}
	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		block = args.get("blocks");
		if (block.equals("2kg")) {
			twokg = 1;
			granite = 6981;
		}
		if (block.equals("5kg")) {
			twokg = 0;
			granite = 6983;
		}
		return true;
	}

	public void serverMessageRecieved(final ServerMessageEvent svr) {
		if (svr.getMessage().indexOf(
				"This block of granite is too small to craft into anything.") != -1) {
			force++;
		}
	}
}